<template>
  <div class="flex pageview">
    <div class="topBox">
      <a href="javascript:;" class="reback" @click="reback"></a>
      <div class="title">个人数据总览</div>
      <div class="time flex flex-align-center">
        <img src="/img/dashboard/time.png" alt="" />
        <span>{{ dateTime }}</span>
      </div>
    </div>
    <div class="bottomBox flex-1 flex">
      <div class="leftbox flex flex-v">
        <!-- 个人基本信息 -->
        <div class="" style="height: 2.46rem">
          <baseInfo></baseInfo>
        </div>
        <!-- 血糖管理 -->
        <div class="flex-1 mt24">
          <glycemicCount></glycemicCount>
        </div>
        <!-- 血压统计 -->
        <div class="flex-1 mt24"><BPCount></BPCount></div>
      </div>
      <div class="centerbox flex-1">
        <div class="flex-1" style="position: relative; background: url(/img/dashboard/person.png) center bottom no-repeat; background-size: contain"></div>
      </div>
      <div class="rightbox flex flex-v">
        <!-- 心电AI预警 -->
        <div class="flex-1">
          <ERAICount :deptId="computedId"></ERAICount>
        </div>
        <!-- 体重体脂 -->
        <div class="flex-1 mt24">
          <weightCount :deptId="computedId"></weightCount>
        </div>
        <!-- 出栏数量 -->
        <div class="flex-1">
          <epidemic :deptId="computedId"></epidemic>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
/* 组件 */
import { baseInfo, glycemicCount, BPCount, ERAICount, weightCount } from "./components/index.js";
import { bigScreenApi } from "@/api/index";

export default {
  name: "GovernmentScreen",
  components: {
    baseInfo,
    glycemicCount,
    BPCount,
    ERAICount,
    weightCount
  },
  data () {
    return {
      timer: "",
      dateTime: "",
      deptId: "",
      areaList: [],
      isCheck: false,
      farmList: [],
      farmName: "",

      replaceFields: {
        children: "childList",
        title: "xzqhmc",
        key: "xzqhbm",
        value: "xzqhbm"
      }
    };
  },
  created () {
    this.timer = null;
    this.dateTime = moment(new Date()).format("YYYY年MM月DD日 HH:mm:ss");
    this.timer = setInterval(() => {
      this.dateTime = moment(new Date()).format("YYYY年MM月DD日 HH:mm:ss");
    }, 1000);
    // this.getareaLink();
  },
  destroyed () {
    clearInterval(this.timer);
  },
  mounted () {
    window.addEventListener("resize", () => {
      // TODO
      // this.resize();
    });
  },
  beforeDestroy () {
    window.removeEventListener("resize");
  },
  computed: {
    computedId () {
      // 等待 apiData 获取到数据后才返回给子组件
      if (this.deptId) {
        return this.deptId;
      } else {
        return "";
      }
    }
  },
  methods: {
    reback () {
      this.$router.back(-1);
    },
    resize () {
      window.location.reload();
    },
    checkFarm () {
      this.isCheck = !this.isCheck;
    },
    // 获取树
    getareaLink () {
      this.$postAction(bigScreenApi.areaLink).then((res) => {
        if (res && res.code == 1) {
          this.farmList = res.data;
          this.goThroughArray(this.farmList);
          this.getName([this.farmList[0]]);
        }
      });
    },
    // 递归循环
    async goThroughArray (arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].level) {
          arr[i].disabled = true;
        }
        if (Array.isArray(arr[i].childList)) {
          // console.log(arr[i]);
          // 如果这个元素是一个数组，递归调用 goThroughArray 函数
          this.goThroughArray(arr[i].childList);
        }
        if (arr[i].farmName) {
          arr[i].xzqhbm = arr[i].xzqhbm + parseInt(Math.random(1) * 1000);
          arr[i].xzqhmc = arr[i].farmName;
        }
      }
    },
    selectName (data, e) {
      this.farmName = e.node.dataRef.xzqhmc;
      this.deptId = e.node.dataRef.deptId;
      console.log(this.farmName, this.deptId);
      this.isCheck = false;
    },
    getName (arr) {
      console.log(arr[0].xzqhmc, "1111");
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].farmName) {
          // console.log(arr[i]);
          this.farmName = arr[i].farmName;
          this.deptId = arr[i].deptId;
        }
        if (Array.isArray(arr[i].childList)) {
          // console.log(arr[i]);
          // 如果这个元素是一个数组，递归调用 goThroughArray 函数
          this.getName(arr[i].childList);
        }
      }
    }
  }
};
</script>

<style scoped lang="less">
@import "./screen.less";
.mt24 {
  margin-top: 0.24rem;
}
</style>
